import { createApp } from "vue";
import { createPinia } from "pinia";
import ElementPlus, { ElMessage } from "element-plus";
import "element-plus/dist/index.css";
import App from "./App.vue";
import router from "./router";
import Cookies from "js-cookie";
import "@/router/dynameAddRouter";
import { i18n } from "@/router/i18n";
import piniaPersist from "pinia-plugin-persistedstate"; // 数据持久化插件
import axios from "axios";
import VueAxios from "vue-axios";
import "./assets/main.css";
import "@/utils/util.ts";
import { useUserStore } from "@/stores/admin/index";

const app = createApp(App);
const pinia = createPinia();
pinia.use(piniaPersist);
app.use(ElementPlus);
app.use(pinia);
app.use(router);
app.use(i18n);
app.use(VueAxios, axios);

app.mount("#app");

router.beforeEach((to, from, next) => {
  if (to.path === "/Login" || Cookies.get("token")) {
    next();
  } else {
    ElMessage.warning("你还没有登录,请前往登录！");
    next("/Login");
  }
});

//注册一个全局自定义指令‘v-permission’
// app.directive('permission',{
//     //当被绑定的元素挂载到 DOM 中时 触发
//     mounted(el,binding){
//         const store = useUserStore()
//         setTimeout(() => {
//             let val = binding.value
//             let text = store.state.personage.user.perms
//             let productList = text || []
//             if(productList.indexOf(val)===-1){
//                 el.remove()
//             }
//         }, 100);
//     }
// })


/**
 * 自定义指令
 */
app.directive('permission', (el, binding) => {
  const store = useUserStore()
  let val = binding.value
  let permissionList = store.PermissionList || []
  if (-1 === permissionList.indexOf(val)) {
    el?.remove()
  }
})
